const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
const appBuildInfos = require('./build-info-processing');
const webpack = require('webpack');


const configs = [];

for (const appBuildInfo of appBuildInfos) {
    const config = {
        entry: {
            index: appBuildInfo.entry,
        },
        module: {
            rules: [
                {
                    test: /\.(js|jsx|ts|tsx)$/i,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            cacheDirectory: true,
                            presets: [
                                [
                                    "@babel/preset-env",
                                    {
                                        useBuiltIns: "usage",
                                        corejs: {
                                            version: "3.25.3",
                                            proposals: true
                                        }
                                    }
                                ],
                                [
                                    "@babel/preset-react",
                                    {
                                        runtime: "automatic"
                                    }
                                ],
                                [
                                    "@babel/preset-typescript",
                                    {
                                        optimizeConstEnums: true,
                                        allExtensions: appBuildInfo.useAllExtension
                                    }
                                ]
                            ]
                        }
                    }
                },
                {
                    test: /\.s[a|c]ss$/i,
                    exclude: /node_modules/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        'css-loader',
                        'postcss-loader',
                        'sass-loader',
                    ]
                },
                {
                    test: /\.css$/i,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: MiniCssExtractPlugin.loader
                        },
                        {
                            loader: 'css-loader',
                            options: {
                                modules: true
                            }
                        },
                        {
                            loader: 'postcss-loader'
                        }


                    ]
                },
                {
                    test: /\.(glsl)$/i,
                    exclude: /node_modules/,
                    type: 'asset/source',
                },
                {
                    test: /\.(mp3|ogg)$/i,
                    type: 'asset/resource',
                    exclude: /node_modules/,
                },
                {
                    test: /\.(mp4|webm)$/i,
                    type: 'asset/resource',
                    exclude: /node_modules/,
                },
                {
                    test: /\.(?:ico|png|jpg|jpeg|gif|hdr)$/i,
                    exclude: /node_modules/,
                    type: 'asset/resource',
                },
                {
                    test: /\.(?:svg)$/i,
                    include: [
                        new RegExp(appBuildInfo.asset),
                    ],
                    exclude: [
                        /node_modules/,
                        new RegExp(appBuildInfo.library),
                        new RegExp(appBuildInfo.src),
                    ],
                    type: 'asset/resource',
                },
                {
                    test: /\.(?:svg)$/i,
                    exclude: [
                        /node_modules/,
                    ],
                    include: [
                        new RegExp(appBuildInfo.library),
                        new RegExp(appBuildInfo.src),
                    ],
                    type: 'asset/inline',
                },

                {
                    test: /\.(woff|woff2|eot|ttf|otf)$/i,
                    exclude: /node_modules/,
                    type: 'asset/resource',
                },
                {
                    test: /\.(csv|tsv)$/i,
                    use: ['csv-loader'],
                    exclude: /node_modules/,
                    type: 'asset/resource',
                },
                {
                    test: /\.xml$/i,
                    use: ['xml-loader'],
                    exclude: /node_modules/,
                    type: 'asset/resource',
                },
                {
                    test: /\.(glb)$/i,
                    exclude: /node_modules/,
                    type: 'asset/resource',
                },
                {
                    test: /\.(json)$/i,
                    exclude: [
                        /node_modules/,
                        new RegExp(appBuildInfo.src),
                    ],
                    type: 'asset/resource',
                },
                {
                    test: /\.(wasm)$/i,
                    type: "asset/inline",
                },
                {
                    test: /\.vue$/i,
                    loader: "vue-loader",
                    options: {
                        transpileOnly: true,
                        compilerOptions: {
                            script: {
                                lang: {
                                    ts: 'babel-loader!ts-loader'
                                }
                            }
                        }
                    }
                },
            ]
        },
        optimization: {
            sideEffects: true,
            moduleIds: 'deterministic',
            runtimeChunk: 'single',
            splitChunks: {
                cacheGroups: {
                    vendor: {
                        test: /[\\/]node_modules[\\/]/,
                        name: 'vendors',
                        chunks: 'all',
                    }
                }
            },
        },
        plugins: [
            new HtmlWebpackPlugin({
                title: appBuildInfo.name,
                filename: "index-original.html",
                template: path.join(__dirname, "./template/index-original.html"),
                inject: 'body',
                scriptLoading: 'defer',
                favicon: appBuildInfo.icon,
            }),
            new HtmlWebpackPlugin({
                title: appBuildInfo.name,
                filename: "index.html",
                template: path.join(__dirname, "./template/index.html"),
                inject: false,
                scriptLoading: 'defer',
                favicon: appBuildInfo.icon,
            }),
            new VueLoaderPlugin(),
            new webpack.DefinePlugin({
                __VUE_OPTIONS_API__: JSON.stringify(true),
                __VUE_PROD_DEVTOOLS__: JSON.stringify(false),
                __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(false)
            })
        ],
        resolve: {
            extensions: ['.tsx', '.ts', '.js', '.jsx', '.jpg', '.png', '.glb', '.hdr', '.svg', '.wasm', 'vue'],
            alias: {
                'three-lib': path.join(__dirname, appBuildInfo.library + '/three-lib'),
                'babylon-lib': path.join(__dirname, appBuildInfo.library + '/babylon-lib')
            },
        },
    };

    configs.push(config);
}


module.exports = configs;